﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;

namespace kindly
{

    public class FileUpdates
    {
        private string fileName;
        private DateTime lastUpdated;
        private int sizeInKb;

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public DateTime LastUpdated
        {
            get { return lastUpdated; }
            set { lastUpdated = value; }
        }

        public int SizeInKb
        {
            get { return sizeInKb; }
            set { sizeInKb = value; }
        }
    }

    public class Files
    {
        public static string GetFilePath(string keyFileName)
        {
            string attchPath = HttpContext.Current.Server.MapPath("~/" + kindly.Properties.Settings.Default.attch_path);
            string fileName = keyFileName.Replace("_", ".");  // we store filenames without dots
            return Path.Combine(attchPath, fileName);
        }

        public static List<FileUpdates> GetFileUpdates(string attachmentPath)
        {
            List<FileUpdates> updates = new List<FileUpdates>();

            DirectoryInfo dinfo = new DirectoryInfo(attachmentPath);

            foreach (FileInfo file in dinfo.GetFiles())
            {
                FileUpdates update = new FileUpdates
                {
                    FileName = file.Name,
                    LastUpdated = file.LastWriteTime,
                    SizeInKb = (int)(file.Length / 1024)
                };
                updates.Add(update);
            }

            return updates;
        }
    }
}